package com.ease.map.handler.mouse {

    import com.ease.map.Map;
    import com.ease.map.geometry.basetype.Pixel;
    import com.ease.map.handler.Handler;
    import flash.events.MouseEvent;

    /**
     * 鼠标滚轮放大缩小地图操作
     */
    public class WheelHandler extends Handler {

        public function WheelHandler(target:Map, handlername:String) {
            super(target, handlername);
        }

        override protected function registerListeners():void {
            if (this.map) {
                this.map.addEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            }
        }

        override protected function unregisterListeners():void {
            if (this.map) {
                this.map.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            }
        }

        private function onMouseWheel(event:MouseEvent):void {
            if (this.map) {
                const px:Pixel = new Pixel(this.map.mouseX, this.map.mouseY);
                const centerPx:Pixel = new Pixel(this.map.width / 2, this.map.height / 2);
                var newCenterPx:Pixel;
                var zoom:Number = this.map.zoom;
                if (event.delta > 0) {
                    zoom++;
                    if (!this.map.isValidZoomLevel(zoom))
                        return;
                    newCenterPx = new Pixel((px.x + centerPx.x) / 2, (px.y + centerPx.y) / 2);
                }
                else {
                    zoom--;
                    if (!this.map.isValidZoomLevel(zoom))
                        return;
                    newCenterPx = new Pixel(2 * centerPx.x - px.x, 2 * centerPx.y - px.y);
                }
                this.map.moveTo(this.map.getLocationFromMapPx(newCenterPx), zoom, false, true);
            }
        }

    }
}
